import resource from '../../lib/resource';
//引入
var util = require("../../utils/login.js");

//配置文件
var config = require("../../config/basicsConfig.js");

const app = getApp();

Page({
  data: {
    display: '',
    userInfo: {},
    order: {
      icon: '',
      text: '我的订单',
      tip: '',
    },
    serverTitle: {
      icon: '',
      text: '我的服务',
      tip: '',
    },
    otherTitle: {
      icon: '',
      text: '其他',
      tip: '',
    },

    // 收货数量
    orderBadge: {
      unpaid: 0,
      undelivered: 0,
      unreceived: 0
    },
    orderCell: [{
        icon: 'images/order.png',
        text: '全部',
        url: 'mybill',
        value: 'all',
        class: 'order-cell-icon-big'
      },{
        icon: 'images/to-be-paid.png',
        text: '已完成',
        url: 'mybill',
        value: 'unpaid',
        class: 'order-cell-icon-small'
      }, {
        icon: 'images/to-be-delivered.png',
        text: '支付失败',
        url: 'mybill',
        value: 'undelivered',
        class: 'order-cell-icon-small',
      }
    ],
    balance:'',
    servicerCell: [{
        icon: 'images/servicecell/ic-me-balance.png',
        text: '我的余额',
        url: 'balance',
        class: 'service-cell-icon-big'
      },
    ],

    otherCell: [{
        icon: 'images/othercell/me-ic-set.png',
        text: '系统设置',
        url: 'configOfSystem',
        class: 'service-cell-icon-big',
        navigateTo: 'navigateTo'
      },
      {
        icon: 'images/othercell/me-ic-phone.png',
        text: '联系电话',
        url: 'phone',
        class: 'service-cell-icon-small'
      }
    ]
  },
  countOrder(orderList) {
    /* eslint no-plusplus: ["error", { "allowForLoopAfterthoughts": true }] */
    this.orderBadge = {
      unpaid: 0,
      undelivered: 0,
      unreceived: 0
    };

    for (let i = orderList.length - 1; i >= 0; i--) {
      switch (orderList[i].order_status) {
        case '待支付':
          this.orderBadge.unpaid += 1;
          break;
        case '待发货':
          this.orderBadge.undelivered += 1;
          break;
        case '待收货':
          this.orderBadge.unreceived += 1;
          break;
        default:
          break;
      }
    }
    this.data.orderCell[0].count = this.orderBadge.unpaid;
    this.data.orderCell[1].count = this.orderBadge.undelivered;
    this.data.orderCell[2].count = this.orderBadge.unreceived;
    this.setData({
      orderBadge: this.orderBadge,
      orderCell: this.data.orderCell
    });
  },
  //点击触发
  onShow() {
    //console.log(app.globalData.userInfo);
    this.setData({
      userInfo: app.globalData.userInfo
    });
    if(this.data.userInfo != null){
      this.getuserinfo();
    }
  },
  onLoad() {
    this.setData({
      userInfo: app.globalData.userInfo
    });
    if(this.data.userInfo != null){
      this.getuserinfo();
    }
  },

  navigateTo(e) {
    const url = e.currentTarget.dataset.url;
    if (e.currentTarget.dataset.urlType) {
      wx.navigateTo({
        url: 'user-info/user-info'
      });
    } else {
      if (url === undefined) {
        wx.makePhoneCall({
          phoneNumber: e.currentTarget.dataset.tip
        });
      } else {
        wx.navigateTo({
          url
        });
      }
    }
  },

  //授权登录
  getUserInfo: function (e) {
    // wx.showLoading({
    //   title: '加载中…'
    // })
    // this.showview();
    //登录
  
    wx.login({
      success: (res) => {
        //获取code
        var param = {};
        param.code = res.code;
        param.avatarUrl = e.detail.userInfo.avatarUrl;
        param.nickName = e.detail.userInfo.nickName;
        //console.log(param);
        //请求
        wx.request({
          url: config.basics.website + config.requesturls.login,
          method: 'POST',
          header: {
            'content-type': 'application/x-www-form-urlencoded',
          },
          data: {
            'code': param.code,
            'nickname': param.nickName,
            'avatarUrl': param.avatarUrl
          },
          success: (res) => {
            //成功
            if (res.data.errCode == 200) {
              let token = res.data.datas.token;
              //let wxuser = res.data.datas.wxuser;
              //缓存token
              wx.setStorageSync("token",token);
              console.log(token);
              var app = getApp();
              app.globalData.userInfo = e.detail.userInfo;
              console.log(e.detail.userInfo);
              this.onLoad();
              wx.hideLoading();
              this.hideview();
            }
          },
          fail(res) {
            //失败
            console.log(res);
          },
        })
      }
    });
  },

  navigator: function (res) {
    console.log(res.currentTarget.dataset);
    if (res.currentTarget.dataset.name == 0) {
      wx.showToast({
        title: '暂未开放，敬请期待',
        icon: 'none',
        duration: 1500 //持续的时间
      });
    } else {
      switch (res.currentTarget.dataset.name) {
        case "phone":
          //拨打电话
          wx.makePhoneCall({
            phoneNumber: "0411-87627772"
          });
          break;

          case "configOfSystem":
            //系统设置
            if(this.data.userInfo == null){
              wx.showToast({
                title: '请重新登录',
                icon: 'none',
                duration: 2000, //持续的时间
              });
              return false;
            }

            wx.navigateTo({
              url: './user-info/user-info',
            })

            break;

        case "mybill":
          //我的订单
          wx.navigateTo({
            url: '../orders/orders?t='+res.currentTarget.dataset.value,
          })
          break;
          case "balance":
            if(this.data.userInfo == null){
              wx.showToast({
                title: '请重新登录',
                icon: 'none',
                duration: 2000, //持续的时间
              });
              return false;
            }
            //我的余额
            wx.navigateTo({
              url: '../balance/balance?t='+res.currentTarget.dataset.value,
            })
            break;
        default:
          break;
      }
    }
    // wx.navigateTo({
    //   url: 'page/des/des'   // 目的页面url
    // })
  },
    //显示
    showview: function() {
      this.setData({
        display: "block"
      })
    },
  //隐藏
    hideview: function() {
      this.setData({
        display: "none"
      })
    },


     /**
   * 获取用户信息
   */
  getuserinfo: function(){
    wx.request({
      url: config.basics.website + config.requesturls.getuserinfo,
      method: 'POST',
      header: {
        'content-type': 'application/x-www-form-urlencoded',
      },
      data: {
        'token': wx.getStorageSync('token'),
      },
      success: (res) => {
           //验证token
           let verificationToken =  util.log.verificationToken(res.data.errCode);
           //console.log('*&*&',verificationToken);
           
           if(verificationToken == false){
            this.onLoad();
           }
           

        //console.log("成功", res);
        this.setData({
          balance: res.data.datas.balance,
        });

      },
      fail: (res) => {
        console.log("失败", res);
      }
    })
  },

});